<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\audit\controller\api;
use app\common\controller\Api;
use think\helper\Arr;

/**
 * 申请记录(我的申请)
 * @package app\Craft
 * @author HCZ<huangchengze@etrol.com>
 */
class Apply extends Api{
	
	/**
	 * 事项列表
	 * @return \think\response\Json
	 */
	public function wfItemList($applyPerson = null,$wftype = null){
		//分页参数
		$where = [];
		
		
		if($applyPerson){
				$where['applyPerson'] =['in',$applyPerson];
		}

		if($wftype){
			$where['wfType'] = $wftype;
		}
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 审核进程列表
			$info = collection(model('Wfitem')->with('workflow,processes')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 审核进程列表
			$info = collection(model('Wfitem')->with('workflow,processes')->where($where)->select())->toArray();
		}
		foreach($info as &$item){
			if(empty($item['processes'])){
				$curr = 0;
			}else{
				if($item['processes'][$item['wfStep']-1]['approvalFlag'] == 0){
					$curr = $item['wfStep']-1;
				}else{
					$curr = $item['wfStep'];
				}
			}
			$progress = floor($curr/$item['workflow']['stepTotal']*100);
			$item['progress'] = $progress;
		}
		
		//总数
		$count = model('Wfitem')->count('id');
		
		if ($count == 0) {
			return json(['code'=>1,'msg'=>'','count'=>$count,'data'=>$info]);
		}
		
		if ($count > 0){
			return json(['code'=>1,'msg'=>'','count'=>$count,'data'=>$info]);
		}
		return json(['code'=>0,'msg'=>'数据异常']);
	}
	
	/**
	 * 流程详情
	 * @param string $itemId
	 */
	public function processList($itemId){
		$wfitem = model('Wfitem')->with('workflow,workflow.steps,workflow.steps.charge')->find($itemId)->toArray();
		$info = model('Wfitem')->with('workflow,processes,processes.wfstep,processes.wfstep.charge')->find($itemId)->toArray();
	
		$info['item'] = $wfitem;
		foreach ($wfitem['workflow']['steps'] as &$step){
			$process = model('Wfprocess')->where('wfItemInfoId',$itemId)->where('wfStepInfoId',$step['id'])->find();
			if(!empty($process)){
				$step['status'] = $process['approvalFlag'];
				$step['approvalDemo'] = $process['approvalDemo'];
				
			}
			$info['process'][] = $step;
			
		}
		return json(['code'=>0,'msg'=>'','count'=>count($info),'data'=>$info]);
		
	}
}